package com.qf.controller;

import com.qf.pojo.Address;
import com.qf.pojo.User;
import com.qf.service.AddressService;
import org.omg.CORBA.INTERNAL;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;

/**
 * @author bss
 * @version V1.0
 * @Project maven
 * @Package com.qf.controller
 * @Description:
 * @Date 2022/10/4 15:43
 */
@RestController
@RequestMapping("/address")
public class AddressController {
    @Autowired
    AddressService addressService;

    @PostMapping("/addAddress")
    public ModelAndView addAddress(HttpSession session,
                           @RequestParam(value = "name")String name,
                           @RequestParam(value = "phone")String phone,
                           @RequestParam(value = "detail")String detail,
                           ModelAndView mav
                           ){
        User user = (User) session.getAttribute("user");
        if (user==null){
            mav.setViewName("redirect:/login.jsp");
            return mav;
        }
        Address address = new Address();
        address.setDetail(detail);
        address.setName(name);
        address.setPhone(phone);
        address.setUid(user.getId());
        address.setLevel(0);
        addressService.addAddress(address);
        mav.setViewName("redirect:/user/getAddress");
        return mav;
    }

    @RequestMapping("/deleteAddress")
    public ModelAndView deleteAddress(HttpSession session, ModelAndView mav, Integer id){
        User user = (User) session.getAttribute("user");
        if (user==null){
            mav.setViewName("redirect:/login.jsp");
            return mav;
        }
        addressService.deleteById(id);
        mav.setViewName("redirect:/user/getAddress");
        return mav;

    }

    @RequestMapping("/defaultAddress")
    public ModelAndView defaultAddress(HttpSession session, ModelAndView mav, Integer id){
        User user = (User) session.getAttribute("user");
        if (user==null){
            mav.setViewName("redirect:/login.jsp");
            return mav;
        }
        addressService.setdefaultAddress(user.getId(),id);
        mav.setViewName("redirect:/user/getAddress");
        return mav;
    }

    @RequestMapping("/updateAddress")
    public ModelAndView updateAddress(HttpSession session,
                                     Address address,
                                      ModelAndView mav) {
        User user = (User) session.getAttribute("user");
        if (user == null) {
            mav.setViewName("redirect:/login.jsp");
            return mav;
        }
        if (address == null) {
            mav.setViewName("redirect:/user/getAddress");
            return mav;
        } else {
            addressService.updateAddress(address);
            mav.setViewName("redirect:/user/getAddress");
            return mav;
        }
    }
}
